//
//  CellForScrollView.m
//  SeeTheWorld
//
//  Created by dllo on 16/1/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CellForScrollView.h"
#import <UIImageView+WebCache.h>
#import "ModelOfScrollView.h"
#import "ViewControllerForClickLunBo.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface CellForScrollView () <UIScrollViewDelegate>

@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIPageControl *pageControl;
@property (nonatomic, retain)NSMutableArray *arrayOfImageView;

/** 控制轮播图 */
@property (nonatomic, retain)NSTimer *timer;

@end

@implementation CellForScrollView

- (void)dealloc {
    self.scrollView.delegate = nil;
    [_timer release];
    [_arrayOfImageView release];
    [_pageControl release];
    [_scrollView release];
    
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
 
        self.scrollView = [[UIScrollView alloc] init];
        [self.contentView addSubview:self.scrollView];
        [_scrollView release];
        self.scrollView.pagingEnabled = YES;
        self.scrollView.delegate = self;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.userInteractionEnabled = YES;
        
        self.pageControl = [[UIPageControl alloc] init];
        [self.contentView addSubview:self.pageControl];
        [_pageControl release];
 
    }
    return self;
}

/** 赋值 */
- (void)passModel:(NSMutableArray *)array {
    
    self.arrayOfImageView = [NSMutableArray array];
    self.arrayOfImageView = array;
    
    /** 前后各加一张照片 */
    [self.arrayOfImageView addObject:self.arrayOfImageView.firstObject];
    [self.arrayOfImageView insertObject:[self.arrayOfImageView objectAtIndex:self.arrayOfImageView.count - 2] atIndex:0];
    
    /** for循环, 铺imageView */
    for (int i = 0; i < array.count; i++) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
        imageView.tag = i + 100;
        ModelOfScrollView *model = array[i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.photo]];
        [self.scrollView addSubview:imageView];
        imageView.userInteractionEnabled = YES;
        
        /** 轮播图添加手势 */
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dianJiLunBo:)];
        [imageView addGestureRecognizer:tap];
        [tap release];
    }
    
    [self start];
}

-(void)start
{
    if (!self.timer) {
        self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(lunBo) userInfo:nil repeats:YES];
    }
    
}

#pragma mark - 轮播图手势实现
- (void)dianJiLunBo:(UITapGestureRecognizer *)tap {
    
    NSInteger i = [tap view].tag - 100;
    ModelOfScrollView *model = self.arrayOfImageView[i];
    
    /** 通知中心传值 */
    [[NSNotificationCenter defaultCenter] postNotificationName:@"passUrl" object:nil userInfo:@{@"content" : model}];
    
}
- (void)lunBo {

    [UIView animateWithDuration:1 animations:^{
        self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x + self.frame.size.width, 0);
        
    } completion:^(BOOL finished) {
        
        [self scrollViewDidEndDecelerating:self.scrollView];
        
        finished = YES;
    }];
    
    
}

/** pageControl跟随轮播图滚动 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if (scrollView.contentOffset.x == self.scrollView.frame.size.width * (self.arrayOfImageView.count - 1)) {
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0)];
    }
    if (scrollView.contentOffset.x == 0) {
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * (self.arrayOfImageView.count - 2), 0)];
    }
    
    
    NSInteger a = self.scrollView.contentOffset.x / WIDTH;
    self.pageControl.currentPage = a - 1;
   
}

/** 布局子视图 */
- (void)layoutSubviews {
    [super layoutSubviews];
   
    self.scrollView.frame = self.contentView.frame;
    self.scrollView.contentSize = CGSizeMake(WIDTH * self.arrayOfImageView.count, 0);
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
    
    self.pageControl.frame = CGRectMake(0, 0, WIDTH / 2, 40);
    self.pageControl.center = CGPointMake(self.scrollView.frame.size.width / 2, self.scrollView.frame.size.height - 20);
    self.pageControl.numberOfPages = 6;
    self.pageControl.currentPage = 0;

}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
